package controller

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
)

var capInstance = base64Captcha.NewCaptcha(base64Captcha.DefaultDriverDigit, base64Captcha.DefaultMemStore)

func Captcha(c *gin.Context) {
	id, b64s, err := capInstance.Generate()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"base64image": b64s,
		"key":         id,
	})
}

func CaptchaVerify(id, answer string, clear bool) bool {
	return capInstance.Verify(id, answer, clear)
}
